package com.ruoyi.familyManagement.service.impl;

import java.util.List;

import com.ruoyi.familyManagement.domain.FamilyUser;
import com.ruoyi.familyManagement.domain.vo.FamilyInfoAndContractedInfo;
import com.ruoyi.familyManagement.domain.vo.FamilyUserInfo;
import com.ruoyi.familyManagement.domain.vo.FamilyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.familyManagement.mapper.DocFamilyMapper;
import com.ruoyi.familyManagement.domain.DocFamily;
import com.ruoyi.familyManagement.service.IDocFamilyService;

/**
 * 家庭表Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@Service
public class DocFamilyServiceImpl implements IDocFamilyService {
    @Autowired
    private DocFamilyMapper docFamilyMapper;

    /**
     * 查询家庭表
     *
     * @param famliyId 家庭表主键
     * @return 家庭表
     */
    @Override
    public DocFamily selectDocFamilyByFamliyId(Long famliyId) {
        return docFamilyMapper.selectDocFamilyByFamliyId(famliyId);
    }

    /**
     * 查询家庭表列表
     *
     * @param docFamily 家庭表
     * @return 家庭表
     */
    @Override
    public List<DocFamily> selectDocFamilyList(DocFamily docFamily) {
        return docFamilyMapper.selectDocFamilyList(docFamily);
    }

    /**
     * 新增家庭表
     *
     * @param docFamily 家庭表
     * @return 结果
     */
    @Override
    public int insertDocFamily(DocFamily docFamily) {
        return docFamilyMapper.insertDocFamily(docFamily);
    }

    /**
     * 修改家庭表
     *
     * @param docFamily 家庭表
     * @return 结果
     */
    @Override
    public int updateDocFamily(DocFamily docFamily) {
        return docFamilyMapper.updateDocFamily(docFamily);
    }

    /**
     * 批量删除家庭表
     *
     * @param famliyIds 需要删除的家庭表主键
     * @return 结果
     */
    @Override
    public int deleteDocFamilyByFamliyIds(Long[] famliyIds) {
        return docFamilyMapper.deleteDocFamilyByFamliyIds(famliyIds);
    }

    /**
     * 删除家庭表信息
     *
     * @param famliyId 家庭表主键
     * @return 结果
     */
    @Override
    public int deleteDocFamilyByFamliyId(Long famliyId) {
        return docFamilyMapper.deleteDocFamilyByFamliyId(famliyId);
    }

    /**
     * 根据用户id查询用户家庭信息
     *
     * @param userId 用户id
     * @return FamilyVO
     */
    @Override
    public FamilyVO findFamilyByUserId(Long userId) {
        return docFamilyMapper.findFamilyByUserId(userId);
    }

    /**
     * 添加家庭信息
     *
     * @param familyVO 家庭信息
     */
    @Override
    public void insertDocFamilyVO(FamilyVO familyVO) {
        docFamilyMapper.insertDocFamilyVO(familyVO);
    }

    /**
     * 添加家庭中间表信息
     *
     * @param userId   用户id
     * @param username 用户账号
     */
    @Override
    public void insertDocFamilyUser(Long userId, String username) {
        FamilyUser familyUser = new FamilyUser();

//        Long familyId = docFamilyMapper.findFamilyIdByUserId(userId);
        Long familyId = docFamilyMapper.findFamilyIdByUserId3(userId);
        familyUser.setFamilyId(familyId);
        familyUser.setUserId(userId);
        familyUser.setUserName(username);

        docFamilyMapper.insertDocFamilyUser(familyUser);
    }

    /**
     * 获取家庭成员信息
     *
     * @param userId 用户id
     * @return FamilyUserInfo
     */
    @Override
    public List<FamilyUserInfo> getFamilyUserInfo(Long userId) {
        Long familyId = docFamilyMapper.findFamilyIdByUserId(userId);
        List<FamilyUserInfo> familyUserInfoList = docFamilyMapper.getFamilyUserInfo(familyId);
        return familyUserInfoList;
    }

    /**
     * 根据用户账号添加家庭成员
     *
     * @param userId   当前用户id
     * @param username 被添加用户账号
     */
    @Override
    public void addFamilyUser(Long userId, String username) {
        // 获取被添加成员的用户id
        Long userId2 = docFamilyMapper.getUserIdByUsername(username);
        // 获取家庭id
        Long familyId = docFamilyMapper.findFamilyIdByUserId(userId);
        // 获取家庭名称
//        String familyName = docFamilyMapper.findFamilyNameByUserId(familyId);
        // 获取团队id
//        Long teamId = docFamilyMapper.getTeamNameByFamilyId(familyId);
        // 添加家庭中间表信息
        FamilyUser familyUser = new FamilyUser();
        familyUser.setFamilyId(familyId);
        familyUser.setUserId(userId2);
        familyUser.setUserName(username);
        docFamilyMapper.insertDocFamilyUser(familyUser);
    }
    /**
     * 根据用户id修改团队id
     */
    public void updateTeamIdByUserId(Long userId, String teamId) {
        docFamilyMapper.updateTeamIdByUserId(userId, teamId);
    }



    /**
     * 校验家庭人数
     *
     * @param userId 当前用户id
     * @return int
     */
    @Override
    public int checkFamilyUserNum(Long userId) {
        int familyUserNum = docFamilyMapper.checkFamilyUserNum(userId);
        return familyUserNum;
    }

    /**
     * 校验用户是否存在
     *
     * @param username 用户名
     * @return boolean
     */
    @Override
    public boolean checkIsExist(String username) {
        Long userId = docFamilyMapper.getUserIdByUsername(username);
        if (userId != null) {
            return true;
        }
        return false;
    }

    /**
     * 校验是否是医生
     *
     * @param username 用户名
     * @return boolean
     */
    @Override
    public boolean checkIsDoctor(String username) {
        Long userId = docFamilyMapper.getUserIdByUsername(username);
        Long roleId = docFamilyMapper.getRoleIdByUserId(userId);
        if (roleId == 102) {
            return true;
        }
        return false;
    }

    /**
     * 校验是否已有团队
     *
     * @param username 用户名
     * @return boolean
     */
    @Override
    public boolean checkIsTeam(String username) {
        Long userId = docFamilyMapper.getUserIdByUsername(username);
        Long teamId = docFamilyMapper.getTeamIdByUserId(userId);
        if (teamId != null) {
            return true;
        }
        return false;
    }

    /**
     * 校验是否已有家庭
     *
     * @param userId 用户id
     * @return int 0-无家庭 1-有家庭
     */
    @Override
    public int findFamilyIdByUserId2(Long userId) {
        return docFamilyMapper.findFamilyIdByUserId2(userId);
    }

    /**
     * 根据用户id查询用户家庭信息和已签约列表信息
     *
     * @param userId 用户id
     * @return List<FamilyInfoAndContractedInfo>
     */
    @Override
    public List<FamilyInfoAndContractedInfo> findFamilyByUserIdAndContracted(Long userId) {
        return docFamilyMapper.findFamilyByUserIdAndContracted(userId);
    }

}
